package com.gdziarmaga.webshop.book.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.gdziarmaga.webshop.author.model.Author;

@Entity
@Table(name = "Book")
public class Book implements java.io.Serializable {
	// TODO przy robieniu update na jakiejś książce nadpisuje mi table
	// Book_Author
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final String ISBN = "isbn";
	public static final String TITLE = "title";
	public static final String ISSUE_YEAR = "issue_year";
	// TODO chyba nie jest potrzebne
	// public static final String SHORT_DESCRIPTION ="short_descripton";
	// public static final String LONG_DECRIPTION="long_descripton";
	public static final String PUBLISHER = "publisher";
	public static final String SECTION = "section";
	public static final String PRICE = "price";

	private long book_id;
	private String isbn;
	private String title;
	private int issue_year;
	private String short_descripton;
	private String long_descripton;
	private int pages;
	// private String icon;
	private String publisher;
	private String section;
	private float price;
	private int number_of_books;

	private Set<Author> author_set;

	// private Set<BookDelivery> book_delivery_set = new
	// HashSet<BookDelivery>();

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "book_seq")
	@SequenceGenerator(name = "book_seq", sequenceName = "BOOK_SEQ")
	@Column(name = "book_id", precision = 20, scale = 0, unique = true)
	public long getBook_id() {
		return book_id;
	}

	public void setBook_id(long bookId) {
		this.book_id = bookId;
	}

	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	@Column(name = "title", nullable = false, length = 100)
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Column(name = "issue_year", nullable = false)
	public int getIssue_year() {
		return issue_year;
	}

	public void setIssue_year(int issue_year) {
		this.issue_year = issue_year;
	}

	@Column(name = "short_description", nullable = false, length = 200)
	public String getShort_descripton() {
		return short_descripton;
	}

	public void setShort_descripton(String short_descripton) {
		this.short_descripton = short_descripton;
	}

	@Column(name = "long_description", nullable = false, length = 20000)
	public String getLong_descripton() {
		return long_descripton;
	}

	public void setLong_descripton(String long_descripton) {
		this.long_descripton = long_descripton;
	}

	@Column(name = "pages", nullable = false)
	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	@Column(name = "publisher", nullable = false, length = 50)
	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	@Column(name = "section", nullable = false, length = 100)
	public String getSection() {
		return section;
	}

	public void setSection(String section) {
		this.section = section;
	}

	@Column(name = "price", nullable = false)
	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	@Column(name = "number_of_books", nullable = false)
	public int getNumber_of_books() {
		return number_of_books;
	}

	public void setNumber_of_books(int number_of_books) {
		this.number_of_books = number_of_books;
	}

	// public void setReview_list(List<Review> review_list) {
	// this.review_list = review_list;
	// }

	// @OneToMany(mappedBy = "book", cascade = CascadeType.ALL)
	// @JoinColumn(name = "book_id_FK")
	// public List<Review> getReview_list() {
	// return review_list;
	// }
	//
	public void setAuthor_set(Set<Author> author_set) {
		this.author_set = author_set;
	}

	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinTable(name = "Book_Author", joinColumns = { @JoinColumn(name = "book_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "author_id", nullable = false, updatable = false) })
	// @ManyToMany(cascade = CascadeType.ALL, mappedBy = "book_list")
	public Set<Author> getAuthor_set() {
		return author_set;
	}

	// public void setBook_delivery_set(Set<BookDelivery> book_delivery_set) {
	// this.book_delivery_set = book_delivery_set;
	// }
	//
	// @OneToMany(mappedBy = "book", cascade = CascadeType.ALL)
	// public Set<BookDelivery> getBook_delivery_set() {
	// return book_delivery_set;
	// }

	// public void setIcon(String icon) {
	// this.icon = icon;
	// }

	// @Column(name = "icon", nullable = true, length = 300)
	// public String getIcon() {
	// return icon;
	// }

	@Transient
	public String getIconURL() {
		return "book_icons/" + getSectionName() + "/small/" + getIsbn()
				+ ".jpg";
	}

	@Transient
	public String getBigIconURL() {
		return "book_icons/" + getSectionName() + "/big/" + getIsbn() + ".jpg";

	}

	@Transient
	private String getSectionName() {
		if (getSection().equals("C/C++")) {
			return "C_C++";
		}
		if (getSection().equals(".NET")) {
			return "dotNET";
		}
		return getSection();
	}

	// public void setAuthor_list(List<Author> author_list) {
	// this.author_list = author_list;
	// }
	//
	// //@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	// @JoinTable(name = "Book_Author", joinColumns = {
	// @JoinColumn(name = "book_id", nullable = false, updatable = false) },
	// inverseJoinColumns = { @JoinColumn(name = "author_id",
	// nullable = false, updatable = false) })
	// @ManyToMany(cascade = CascadeType.ALL, mappedBy = "book_set")
	// public List<Author> getAuthor_list() {
	// return author_list;
	// }

	@Transient
	public List<Author> getAuthor_list() {
		Object[] objects = getAuthor_set().toArray();
		Author[] authors = new Author[objects.length];

		for (int t = 0; t < objects.length; t++) {
			authors[t] = (Author) objects[t];
		}
		List<Author> auList = new ArrayList<Author>(Arrays.asList(authors));
		return auList;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (book_id ^ (book_id >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Book other = (Book) obj;
		if (book_id != other.book_id)
			return false;
		return true;
	}

}
